package com.habook.ui;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class HaEntrance {

	public static void main(String[] args) {
		String version = System.getProperty("java.version");
		if (version.compareTo("1.6.0") < 0) {
			int type = JOptionPane.ERROR_MESSAGE;
			String title = "Error Message";
			String message = String.format("Current java version(%s), must >= 1.6.0", version);
			JOptionPane.showMessageDialog(null, message, title, type);
		} else {
			InstanceControl ic = new InstanceControl();
			ic.start();
			JFrame frame = HaFrame.Main(args);
			ic.setFrame(frame);
		}
	}

}

class InstanceControl extends Thread {
	private JFrame frame = null;

	public void run() {
		int[] ports = { 43695, 51473, 62584, 47079, 53496 };

		try {
			for (int i = 0; i < ports.length; i++)
				new Socket("127.0.0.1", ports[i]);
			throw new RuntimeException("An instance already running!");
		} catch (RuntimeException re) {
			System.err.println(re.toString());
			System.exit(0);
		} catch (Exception e) {
		}

		for (int i = 0; i < ports.length; i++) {
			final int port = ports[i];
			new Thread() {
				public void run() {
					try {
						ServerSocket server = new ServerSocket(port);
						while (true) {
							server.accept();
							if (frame != null) {
								frame.setVisible(true);
								frame.requestFocus();
								frame.requestFocusInWindow();
								frame.toFront();
								frame.setState(JFrame.NORMAL);
							}
						}
					} catch (IOException ioe) {
					}
				}
			}.start();
		}
	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

}
